package com.example.customview.scrollevent.more

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.ListView
import android.widget.ScrollView

class OutScrollerView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defaultStyle: Int = 0) :
    ScrollView(context, attributeSet, defaultStyle) {
    /**
     * 外部拦截法
     */
    private var mLastY = 0f
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        super.onInterceptTouchEvent(ev)
        var interceptor = false

        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                interceptor = false
            }
            MotionEvent.ACTION_MOVE -> {
             val listView =   ( getChildAt(0) as ViewGroup).getChildAt(1) as ListView
                if (listView.firstVisiblePosition == 0 &&(ev.y -mLastY>0)) {
                    interceptor = true
                }else if(listView.lastVisiblePosition == listView.count-1 && (ev.y -mLastY<0)){
                    interceptor = true
                }else{
                    interceptor = false
                }
            }
            MotionEvent.ACTION_UP -> {
                interceptor = false
            }

        }
        mLastY = ev!!.y
        return interceptor
    }

    // 内部拦截法
 /*   override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        // down事件的时候不能拦截，因为这个时候 requestDisallowInterceptTouchEvent 无效
        if (event.action == MotionEvent.ACTION_DOWN) {
            super.onInterceptTouchEvent(event)
            return false
        }
        return true
    }*/



}